package com.lin.app.midtermtest.jo6num3;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;

public class Webkit extends Activity{
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webkit);
        init();
        webview.loadUrl(url);
        
    }
    private void init(){
    	webview = (WebView)findViewById(R.id.WebView);
    	back = (Button)findViewById(R.id.ButtonBcak);
    	exit = (Button)findViewById(R.id.ButtonExit);
    	cnu = (Button)findViewById(R.id.ButtonCNU);
    	mClient = new MyWebClient();
    	
    	
    	webview.getSettings().setJavaScriptEnabled(true);
    	webview.setWebViewClient(mClient);
    	cnu.setOnClickListener(new OnClickListener(){

			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				webview.loadUrl("http://www.cnu.ac.kr");
			}});
    	exit.setOnClickListener(new OnClickListener(){

			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				Bundle b = new Bundle();
				b.putString(LINMidtermTestJo6Num3.MYWEB_TAG, webview.getUrl());
				
				intent.putExtras(b);
				Webkit.this.setResult(RESULT_OK,intent);
				
				Webkit.this.finish();
			}});
    	back.setOnClickListener(new OnClickListener(){

			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				webview.goBack();
			}});
    	
    }
    private class MyWebClient extends WebViewClient {
    	public boolean shouldOverrideUrlLoading(WebView view, String url) {  
    		view.loadUrl(url);       
    		return true;    
    		}
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {  
    	if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) { 
    		webview.goBack();      
    		return true;    }   
    	return super.onKeyDown(keyCode, event);
    }
	
    @Override
    protected void onNewIntent (Intent intent) {
    	uri = intent.getData();
    	url = uri.toString();
    	isIntent = true;
    	log(url);
    	
    	this.onResume();
    }
    private void log(String s){
    	Log.i("test",s);
    }
    
    
    private Uri uri;
    private String url = "http://www.baidu.com";
    private boolean isIntent = false;
    private WebView webview;
    private Button back,exit,cnu;
    private MyWebClient mClient;
}
